﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Data;
using StoragePeriod.Common;

namespace StoragePeriod.Web.UserControls
{
    public partial class NutrientsintroTree : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            BindTreeView();
        }

        public void NutrientsintroTree_SelectedNodeChanged(object sender, EventArgs e)
        {
            //SetClientSelectedOrgValue();
            //if (SelectedChanging != null)
            //{
            //    Dictionary<string, string> dic = new Dictionary<string, string>();
            //    NutrientsTree.CheckedNodes.AllNodes().Where(p => p.Target == "1").ToList().ForEach(p =>
            //    {
            //        if (!dic.ContainsKey(p.Value))
            //        {
            //            dic.Add(p.Value, p.Text);
            //        }
            //    });
            //    SelectedChanging(dic);
            //}
        }

        public void BindTreeView()
        {
            NutrientsTree.Nodes.Clear();            
            NutritionType.All().ToList().ForEach(p =>
            {
                TreeNode childNode = new TreeNode()
                {
                    Text = p.nutrition,
                    Value = p.id.EToString()
                };
                List<IntroduceNutrition> introlist = IntroduceNutrition.Where("typeId == @0", p.id).ToList();
                introlist.ForEach(a =>
                {
                    childNode.ChildNodes.Add(new TreeNode()
                    {
                        Text = a.title,
                        Value = a.id.EToString()
                    });
                });
                if (!NutrientsTree.Nodes.Contains(childNode)) NutrientsTree.Nodes.Add(childNode);
            });
            //SetClientSelectedOrgValue();
            ////treeViewSelectValue.Value = String.Format("{0}-{1}", NutrientsTree.SelectedNode.Value, NutrientsTree.SelectedNode.Text);
        }


        private void SetClientSelectedOrgValue()
        {

            treeViewSelectValue.Value = String.Format("{0}-{1}", NutrientsTree.SelectedNode.Value, NutrientsTree.SelectedNode.Text);
        }
    }
}